@echo off
chcp 65001 >nul
cd /d "%~dp0"

:: Create debug log
echo [%time%] Starting debug install >> debug_install.log

echo ========================================
echo    Debug Installing Floating Overlay
echo ========================================
echo.

echo [DEBUG] Current directory: %CD%
echo [DEBUG] Script location: %~dp0
echo [%time%] Script started in: %CD% >> debug_install.log

echo [1/5] Checking Node.js...
echo [%time%] Checking Node.js >> debug_install.log

node --version >nul 2>&1
if %errorLevel% neq 0 (
    echo [ERROR] Node.js not found!
    echo [%time%] ERROR: Node.js not found, errorLevel: %errorLevel% >> debug_install.log
    echo [DEBUG] PATH: %PATH% >> debug_install.log
    echo [TIPS] Please install Node.js first from: https://nodejs.org/
    echo.
    echo Press any key to exit...
    pause >nul
    exit /b 1
) else (
    for /f "tokens=*" %%i in ('node --version') do (
        echo [SUCCESS] Node.js: %%i
        echo [%time%] Node.js version: %%i >> debug_install.log
    )
)

echo [2/5] Checking npm...
echo [%time%] Checking npm >> debug_install.log

npm --version >nul 2>&1
if %errorLevel% neq 0 (
    echo [ERROR] npm not found!
    echo [%time%] ERROR: npm not found >> debug_install.log
    echo.
    echo Press any key to exit...
    pause >nul
    exit /b 1
) else (
    for /f "tokens=*" %%i in ('npm --version') do (
        echo [SUCCESS] npm: %%i
        echo [%time%] npm version: %%i >> debug_install.log
    )
)

echo [3/5] Checking package.json...
echo [%time%] Checking package.json >> debug_install.log

if not exist "package.json" (
    echo [ERROR] package.json not found!
    echo [%time%] ERROR: package.json not found >> debug_install.log
    echo [DEBUG] Current directory contents:
    dir /b
    echo.
    echo Press any key to exit...
    pause >nul
    exit /b 1
) else (
    echo [SUCCESS] package.json found
    echo [%time%] package.json found >> debug_install.log
    echo [DEBUG] package.json content:
    type package.json
)

echo [4/5] Cleaning old installation...
echo [%time%] Cleaning old installation >> debug_install.log

if exist "node_modules" (
    echo [INFO] Removing old node_modules...
    echo [%time%] Removing old node_modules >> debug_install.log
    rmdir /s /q node_modules 2>>debug_install.log
)

if exist "package-lock.json" (
    echo [INFO] Removing package-lock.json...
    echo [%time%] Removing package-lock.json >> debug_install.log
    del package-lock.json 2>>debug_install.log
)

echo [5/5] Installing dependencies...
echo [%time%] Installing dependencies >> debug_install.log
echo [INFO] This may take several minutes...
echo [DEBUG] Running: npm install --verbose

npm install --verbose >>debug_install.log 2>&1
set INSTALL_EXIT_CODE=%errorLevel%

echo [%time%] npm install finished with code %INSTALL_EXIT_CODE% >> debug_install.log

if %INSTALL_EXIT_CODE% neq 0 (
    echo [ERROR] Failed to install dependencies!
    echo [DEBUG] Exit code: %INSTALL_EXIT_CODE%
    echo [DEBUG] Last 20 lines of log:
    echo ======================== LOG TAIL ========================
    powershell "Get-Content debug_install.log | Select-Object -Last 20"
    echo ======================== END LOG ========================
    echo.
    echo [TIPS] Try the following:
    echo   1. Check network connection
    echo   2. Run: npm cache clean --force
    echo   3. Check antivirus software
    echo   4. Try running as administrator
    echo   5. Check full debug_install.log file
    echo.
    echo Press any key to exit...
    pause >nul
    exit /b 1
) else (
    echo [SUCCESS] Dependencies installed successfully!
    echo [%time%] Dependencies installed successfully >> debug_install.log
)

echo [CHECK] Verifying installation...
echo [%time%] Verifying installation >> debug_install.log

if exist "node_modules\electron" (
    echo [SUCCESS] Electron found!
    echo [%time%] Electron found >> debug_install.log
    
    :: Get Electron version
    for /f "tokens=*" %%i in ('node -e "console.log(require('./node_modules/electron/package.json').version)"') do (
        echo [INFO] Electron version: %%i
        echo [%time%] Electron version: %%i >> debug_install.log
    )
) else (
    echo [WARNING] Electron may not be installed correctly
    echo [%time%] WARNING: Electron not found >> debug_install.log
)

if exist "node_modules\node-fetch" (
    echo [SUCCESS] node-fetch found!
    echo [%time%] node-fetch found >> debug_install.log
) else (
    echo [WARNING] node-fetch may not be installed correctly
    echo [%time%] WARNING: node-fetch not found >> debug_install.log
)

echo.
echo ========================================
echo     Installation Completed!
echo ========================================
echo.
echo [SUCCESS] Floating overlay is ready!
echo [%time%] Installation completed >> debug_install.log
echo [NEXT STEP] Run start_overlay.bat to launch the overlay
echo [DEBUG] Check debug_install.log for detailed information
echo.
echo Press any key to exit...
pause >nul
